package com.mydb.core;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;

import com.google.gson.Gson;
import com.mydb.core.util.DBUtils;

public class MyDBEngine {

	/**
	 * 创建数据库
	 * @param path
	 * @throws Exception
	 */
	public void createDataBase(String path) throws Exception {
		FileOutputStream fis = new FileOutputStream(path);
		Document doc = new Document();
		Element root = new Element("database");
		Element table = new Element("table");
		table.setAttribute("name", "table1");
		Element indexs = new Element("table");
		indexs.setAttribute("name", "system.indexs");
		Element users = new Element("table");
		users.setAttribute("name", "system.users");

		root.addContent(table);
		root.addContent(indexs);
		root.addContent(users);

		doc.setRootElement(root);
		XMLOutputter out = new XMLOutputter();
		out.output(doc, fis);
	}
	
	/**
	 * 得到制定表数据
	 * @param path
	 * @param tablename
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	public List<Map> queryDatas(String path, String tablename) throws JDOMException, IOException {
		List<Map> retlist = new ArrayList<Map>();
		Document doc = DBUtils.getDocument(path);
		Element root = doc.getRootElement();
		List<Element> list = root.getChildren();
		if(list != null && list.size() > 0){
			for(Element e:list){
				String tname = e.getAttributeValue("name");
				if(tablename.equals(tname)){
					List<Element> datalist = e.getChildren();
					if(datalist != null && datalist.size() > 0){
						for(Element data:datalist){
							Map map = new Gson().fromJson(data.getText(), Map.class);
							retlist.add(map);
						}
					}
				}
			}
		}
		return retlist;
	}
	
	/**
	 * 添加一个对象
	 * @param path
	 * @param tablename
	 * @param o
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	public int addData(String path, String tablename, Object o) throws JDOMException, IOException{
		Gson gson = new Gson();
		Document doc = DBUtils.getDocument(path);
		Element root = doc.getRootElement();
		List<Element> list = root.getChildren();
		if(list != null && list.size() > 0){
			for(Element e:list){
				String tname = e.getAttributeValue("name");
				if(tablename.equals(tname)){
					Element data = new Element("data");
					
					Map map = gson.fromJson(gson.toJson(o), Map.class);
					map.put("_uuid", UUID.randomUUID());
					data.addContent(gson.toJson(map));
					e.addContent(data);
				}
			}
			XMLOutputter out = new XMLOutputter();
			out.output(doc, new FileOutputStream(path));
			return 1;
		}
		return 0;
	}
	
	/**
	 * 更新一个对象
	 * @param path
	 * @param tablename
	 * @param o
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	public int updataData(String path, String tablename, Object o) throws JDOMException, IOException{
		Gson gson = new Gson();
		Document doc = DBUtils.getDocument(path);
		Element root = doc.getRootElement();
		List<Element> list = root.getChildren();
		if(list != null && list.size() > 0){
			for(Element e:list){
				String tname = e.getAttributeValue("name");
				if(tablename.equals(tname)){
					Map newdata = gson.fromJson(gson.toJson(o), Map.class);
					String newid = (String) newdata.get("_uuid");
					
					List<Element> datas = e.getChildren();
					if(datas != null && datas.size() > 0){
						for(Element data:datas){
							String oldstr = data.getText();
							Map olddata = gson.fromJson(oldstr, Map.class);
							String oldid = (String) olddata.get("_uuid");
							if(oldid.equals(newid)){
								e.removeContent(data);
								Element updata = new Element("data");
								updata.setText(gson.toJson(newdata));
								e.addContent(updata);
								break;
							}
						}
					}
				}
			}
			XMLOutputter out = new XMLOutputter();
			out.output(doc, new FileOutputStream(path));
			return 1;
		}
		return 0;
	}
	
	/**
	 * 删除对象
	 * @param path
	 * @param tablename
	 * @param o
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	public int deleteData(String path, String tablename, Object o) throws JDOMException, IOException{
		Gson gson = new Gson();
		Document doc = DBUtils.getDocument(path);
		Element root = doc.getRootElement();
		List<Element> list = root.getChildren();
		if(list != null && list.size() > 0){
			for(Element e:list){
				String tname = e.getAttributeValue("name");
				if(tablename.equals(tname)){
					Map deldata = gson.fromJson(gson.toJson(o), Map.class);
					String delid = (String) deldata.get("_uuid");
					
					List<Element> datas = e.getChildren();
					if(datas != null && datas.size() > 0){
						for(Element data:datas){
							String oldstr = data.getText();
							Map olddata = gson.fromJson(oldstr, Map.class);
							String oldid = (String) olddata.get("_uuid");
							if(oldid.equals(delid)){
								e.removeContent(data);
								break;
							}
						}
					}
				}
			}
			XMLOutputter out = new XMLOutputter();
			out.output(doc, new FileOutputStream(path));
			return 1;
		}
		return 0;
	}
}
